package com.yntx.usercenter.common.web;

import lombok.Getter;
import lombok.Setter;

/**
 * Description: 接口返回实体类包装
 *
 * @author Liziba
 * @version 1.0
 * @date 2022/9/22 2:50 PM
 * @since JDK 1.8
 */
@Getter
@Setter
public class ApiResult<T> {

    /**
     * 是否成功
     */
    private boolean flag;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    private ApiResult() {

    }

    private ApiResult(boolean flag, Integer code, String message, T data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ApiResult<T> ofSuccess() {
        return ofSuccess(null);
    }

    public static <T> ApiResult<T> ofSuccess(T data) {
        return ofSuccess(null, data);
    }

    public static <T> ApiResult<T> ofSuccess(String message, T data) {
        return of(true, Status.Common.SUCCESS.getCode(), null, data);
    }

    public static <T> ApiResult<T> ofFail() {
        return ofStatus(false, Status.Common.ERROR);
    }

    public static <T> ApiResult<T> ofFail(String message) {
        return ofStatus(false, Status.Common.ERROR, message);
    }

    public static <T> ApiResult<T> ofFail(Integer code, String message) {
        return of(false, code, message, null);
    }

    public static <T> ApiResult<T> ofFailStatus(Status status) {
        return of(false, status.getCode(), status.getMessage(), null);
    }

    public static <T> ApiResult<T> ofStatus(boolean flag, Status status) {
        return of(flag, status.getCode(), status.getMessage(), null);
    }

    public static <T> ApiResult<T> ofStatus(boolean flag, Status status, T data) {
        return of(flag, status.getCode(), status.getMessage(), data);
    }

    public static <T> ApiResult<T> ofStatus(boolean flag, Status status, String message) {
        return of(flag, status.getCode(), message, null);
    }

    public static <T> ApiResult<T> ofStatus(boolean flag, Status status, String message, T data) {
        return of(flag, status.getCode(), message, data);
    }

    /**
     * 构造一个完全自定义的API返回结构
     *
     * @param flag
     * @param code
     * @param message
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ApiResult<T> of(boolean flag, Integer code, String message, T data) {
        return new ApiResult<>(flag, code, message, data);
    }

}
